package controller;

import java.util.Observable;
import java.util.Observer;

import model.CameraManager;
import model.PreviewCallback;
//import view.CameraPreviewView;



import com.vandorenmes.argus.R;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;

public class Scanner extends Activity implements Observer{
    
    //Handles the information of a QRCode 
    
    private CaptureController captureController;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scanner);
        
        ((App)getApplication()).setScanner(this);

        captureController = new CaptureController(this);
        
        CameraManager.getInstance().requestNextFrame(new PreviewCallback(captureController.getCaptureHandler(), getApplicationContext()));
    }
    
    //this method is called when the android application resumes from a pause.
    @Override
    protected void onResume() 
    {
    	Log.v("RESUME", "OnResume()");
    	super.onResume();
    	CameraManager.getInstance().reconnectCamera();
    	CameraManager.getInstance().requestNextFrame(new PreviewCallback(captureController.getCaptureHandler(), getApplicationContext()));
    }

    // this method is called last before the application pauses.
    @Override
    protected void onPause() {
    	Log.v("PAUSE", "OnPause()");
        super.onPause();
        CameraManager.getInstance().release();
        findViewById(R.id.machineView).setVisibility(View.GONE);                
    }

	@Override
	public void update(Observable observable, Object data) 
	{
		// TODO Auto-generated method stub
		
	}

//    // this class is used to communicate with the captureHandler
//    private class OnDecoded implements CaptureHandler.OnDecodedCallback 
//    {    	
//        @Override
//        public void onDecoded(String decodedData) 
//        {
//        	Log.v("QR", decodedData);
//            ((TextView) findViewById(R.id.lblMultiQR)).setText(decodedData);
//        }
//
//		@Override
//		public void onLoading() 
//		{
//			findViewById(R.id.loader).setVisibility(View.VISIBLE);
//		}
//
//		public void onWcfResponse(String decodedData) 
//		{
//			findViewById(R.id.loader).setVisibility(View.GONE);
//			TextView lbl = ((TextView) findViewById(R.id.lblMultiQR));
//			
//			lbl.setText(decodedData);
//			
//			boolean decodedDataIsEmpty = decodedData.equals("");
//			
//			if(!decodedDataIsEmpty)
//			{
////				lbl.setText(decodedData);
//				findViewById(R.id.machineView).setVisibility(View.GONE);
//				return;
//			}
////			else
////			{
////				lbl.setText("");
////			}
//			
//			App app = (App) getApplication();	
//			Machine machine = app.getCurrentMachine();
//			
//			Log.v("wcf", String.format("QR-code: %s", app.getCurrentQR()));
//			
//			if(machine != null)
//			{
//				fillLabelWithMachineInfo(machine);
//			}
//			else 
//			{
//				lbl.setText("ID unknown");
//				findViewById(R.id.machineView).setVisibility(View.GONE);
//			}
//		}
//		
//		private void fillLabelWithMachineInfo(Machine machine)
//		{
//			Log.v("wcf", "machine: " + machine.toString());
//			((TextView) findViewById(R.id.id_val)).setText(machine.getID());
//			((TextView) findViewById(R.id.name_val)).setText(machine.getName());
//			((TextView) findViewById(R.id.part_val)).setText(machine.getPart());
//			((TextView) findViewById(R.id.company_val)).setText(machine.getCompany());
//			((TextView) findViewById(R.id.desc_val)).setText(machine.getDescription());
//			((TextView) findViewById(R.id.status_val)).setText(machine.getStatus());			
//			
//			findViewById(R.id.machineView).setVisibility(View.VISIBLE);
//		}
//    }
//    
}
